//
//  ZH_HomeLiveViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/25.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_HomeLiveViewController.h"
#import "VTMagic.h"
#import "ZH_HomeLiveInfoViewController.h"
#import "ZH_ClassViewModel.h"
#import "ZH_HomeLiveInfoViewController.h"
@interface ZH_HomeLiveViewController ()<VTMagicViewDelegate,VTMagicViewDataSource>
@property (nonatomic, strong) VTMagicController *magicController;
@property (nonatomic, strong) NSMutableArray *menuList;
@property (nonatomic,weak)UIView *bgView;
@property (nonatomic,weak)UILabel *label;
@property (nonatomic,weak)UILabel *yearLabel;
@property (nonatomic,weak)UIImageView *iconImage;
@property (nonatomic,weak)UIView *bgViews;
@property (nonatomic,weak)UIButton *button;
@property (nonatomic,strong)ZH_ClassViewModel *viewModel;
@property (nonatomic,strong)Hen_CustomPickerView *pickerView;
@property (nonatomic,strong)NSMutableArray<Hen_CustomPickerModel*> *gradeArray;
@property (nonatomic,strong)NSString *gradeString;
@end

@implementation ZH_HomeLiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    
    [self initDataMeulist];
    
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.gradeString = @"1";
    [self gradeInitData];
}
- (void)initUserInterface{
    self.navigationItem.title = @"直播";
    self.view.backgroundColor = kCommonBackgroudColor;
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(FITSCALE(41));
    }];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(FITSCALE(14));
        make.left.equalTo(self.view).offset(FITSCALE(22));
    }];
    [self.yearLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(FITSCALE(14));
        make.right.equalTo(self.view).offset(FITSCALE(-44));
    }];
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.yearLabel);
        make.right.equalTo(self.view).offset(FITSCALE(-16));
    }];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.yearLabel);
        make.centerY.equalTo(self.bgView);
        make.right.equalTo(self.iconImage);
    }];
    [self.bgViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_bottom).offset(FITSCALE(9));
        make.left.right.bottom.equalTo(self.view);
    }];
    //    self.edgesForExtendedLayout = UIRectEdgeAll;
    [self addChildViewController:self.magicController];
    [self.bgViews addSubview:_magicController.view];
    [self.bgViews setNeedsUpdateConstraints];
    [_magicController.magicView reloadData];
    
}
- (void)initDataMeulist{
    WEAKSelf;
    [self.viewModel studentGetcateResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [self.menuList removeAllObjects];
            for (NSInteger i = 0;i < self.viewModel.classArray.count;i ++) {
                [self.menuList addObject:self.viewModel.classArray[i][@"name"]];
            }
            [self.magicController.magicView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
- (void)gradeInitData{
    WEAKSelf;
    [self.viewModel studentGetgradeResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [self.gradeArray removeAllObjects];
            for (NSInteger i = 0;i < self.viewModel.gradeArray.count;i ++) {
                Hen_CustomPickerModel *data = [[Hen_CustomPickerModel alloc] initWithDictionary:@{}];
                data.itemId = [NSString stringWithFormat:@"%@",self.viewModel.gradeArray[i][@"id"]];
                data.name = self.viewModel.gradeArray[i][@"grade"];
                [weakSelf.gradeArray addObject:data];
            }
            weakSelf.gradeString = [NSString stringWithFormat:@"%@",self.viewModel.gradeArray[0][@"id"]];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
// 菜单标题
- (NSArray<NSString *> *)menuTitlesForMagicView:(VTMagicView *)magicView {
    return self.menuList;
}

// 点击时button变化
- (UIButton *)magicView:(VTMagicView *)magicView menuItemAtIndex:(NSUInteger)itemIndex {
    static NSString *itemIdentifier = @"itemIdentifier";
    UIButton *menuItem = [magicView dequeueReusableItemWithIdentifier:itemIdentifier];
    if (!menuItem) {
        menuItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [menuItem setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [menuItem setTitleColor:[UIColor colorWithHexString:@"#01CAAF"] forState:UIControlStateSelected];
        menuItem.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:15.f];
    }
    return menuItem;
}
// 点击跳转的视图控制器
- (UIViewController *)magicView:(VTMagicView *)magicView viewControllerAtPage:(NSUInteger)pageIndex {
    
    ZH_HomeLiveInfoViewController *vc = [[ZH_HomeLiveInfoViewController alloc] init];
    vc.cateString = [NSString stringWithFormat:@"%@",self.viewModel.classArray[pageIndex][@"id"]];
    vc.gradeString = self.gradeString;
    vc.typeString = @"3";
    vc.hidesBottomBarWhenPushed = YES;
    return vc;
}
- (void)button:(UIButton *)btn{
    [self.pickerView showPickerViewWithDataSource:@[self.gradeArray] unitArray:nil];
}
#pragma mark - accessor methods
- (VTMagicController *)magicController {
    if (!_magicController) {
        _magicController = [[VTMagicController alloc] init];
        _magicController.view.translatesAutoresizingMaskIntoConstraints = YES;
        _magicController.magicView.navigationColor = [UIColor whiteColor];//导航栏的颜色
        _magicController.magicView.sliderColor = [UIColor colorWithHexString:@"#01CAAF"];//滑动条的颜色
        _magicController.magicView.switchStyle = VTSwitchStyleDefault;
        _magicController.magicView.layoutStyle = VTLayoutStyleDefault;
        //        _magicController.magicView.navigationHeight = 64.f;
        _magicController.magicView.headerHidden = YES;
        //        _magicController.magicView.headerHeight = [DATAMODEL KNavigationBarHeioght];
        _magicController.magicView.againstStatusBar = NO;
        _magicController.magicView.sliderExtension = 10.0;
        _magicController.magicView.dataSource = self;
        _magicController.magicView.delegate = self;
        _magicController.magicView.itemScale = 1.1;//点中的item变大
    }
    return _magicController;
}

- (NSMutableArray *)menuList{
    if (!_menuList) {
        _menuList = [[NSMutableArray alloc] initWithCapacity:0];
        //      [_menuList addObjectsFromArray:@[@"文学",@"音乐",@"建筑",@"舞蹈",@"戏曲",@"绘画",@"其他"]];
    }
    return _menuList;
}

#pragma mark - getter,setter
- (UIView *)bgView{
    if (!_bgView) {
        UIView *bgView = [UIView createViewWithBackgroundColor:kFontColorWhite];
        [self.view addSubview:_bgView = bgView];
    }
    return _bgView;
}
- (UIView *)bgViews{
    if (!_bgViews) {
        UIView *bgView = [UIView createViewWithBackgroundColor:kFontColorWhite];
        [self.view addSubview:_bgViews = bgView];
    }
    return _bgViews;
}
- (UILabel *)label{
    if (!_label) {
        UILabel *label = [UILabel createLabelWithText:@"课程分类" font:kFontSize_28];
        [self.view addSubview:_label = label];
    }
    return _label;
}
- (UILabel *)yearLabel{
    if (!_yearLabel) {
        UILabel *label = [UILabel createLabelWithText:@"1年级" font:kFontSize_28];
        label.textColor = [UIColor colorWithHexString:@"#00C9AE"];
        label.textAlignment = NSTextAlignmentRight;
        [self.view addSubview:_yearLabel = label];
    }
    return _yearLabel;
}
- (UIImageView *)iconImage{
    if (!_iconImage) {
        UIImageView *image = [UIImageView createImageViewWithName:@"icon_jbxl"];
        [self.view addSubview:_iconImage = image];
    }
    return _iconImage;
}
- (ZH_ClassViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_ClassViewModel alloc] init];
    }
    return _viewModel;
}
- (Hen_CustomPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[Hen_CustomPickerView alloc] init];
        WEAKSelf;
        _pickerView.onCustomPickerSelectBlock = ^(NSMutableDictionary *selectedDict) {
            Hen_CustomPickerModel *model = selectedDict[@"0"];
            weakSelf.yearLabel.text = model.name;
            weakSelf.gradeString = model.itemId;
            [weakSelf.magicController.magicView reloadData];
        };
    }
    return _pickerView;
}
- (NSMutableArray<Hen_CustomPickerModel *> *)gradeArray{
    if (!_gradeArray) {
        _gradeArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _gradeArray;
}
- (UIButton *)button{
    if (!_button) {
        UIButton *button = [UIButton createNoBgButtonWithTitle:@"" target:self action:@selector(button:)];
        button.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_button = button];
    }
    return _button;
    
}
@end
